#!/bin/bash

. config.py

efi_cfg=${ISO_ROOT}${GRUB2_PREFIX}/grub2_efi_common.cfg
pc_cfg=${ISO_ROOT}${GRUB2_PREFIX}/grub2_pc_common.cfg
grub_menu=${ISO_ROOT}/grub4dos/menu.lst
grub_menu_tmp=${grub_menu}.tmp

rm -rf $efi_cfg $pc_cfg

if test -f ${grub_menu};then
    echo "####insert by genrubcfg  --------start" >${grub_menu_tmp}
fi

iso_root_exists()
{
    local f o
    for f in ${ISO_ROOT} ${EXTRA_ROOTS}
    do
        o=$(echo ${f}/$1|head -n1)
        if test -f ${o};then
            idfile=$(echo $o|sed "s,${f}/,,g")
            return 0;
        fi
    done
    return 1
}



### Windows7PE
idfile=/7777/7PE_MGR
tmpfile=/tmp/.tmp_grub.cfg
if iso_root_exists $idfile && test -f ${grub_menu};then
cat <<__eof__ >>${tmpfile}

title Windows7PE
    find --set-root  --ignore-floppies  $idfile
    chainloader ${idfile}

__eof__
cat ${tmpfile} >> ${grub_menu_tmp}
fi
rm -f ${tmpfile}



### Windows2003PE
idfile=/WXPE/SETUPLDR.BIN
tmpfile=/tmp/.tmp_grub.cfg
if iso_root_exists $idfile  && test -f ${grub_menu};then
cat <<__eof__ >>${tmpfile}

title Windows2003PE
    find --set-root  --ignore-floppies  $idfile
    chainloader ${idfile}

__eof__
cat ${tmpfile} >> ${grub_menu_tmp}
fi
rm -f ${tmpfile}



### CDLinux
idfile=/CDlinux/initrd
tmpfile=/tmp/.tmp_grub.cfg
if iso_root_exists $idfile ;then
cat <<__eof__ >>${tmpfile}
    menuentry "CDLinux" {
        search --file --no-floppy --set=root $idfile
        linux /CDlinux/bzImage CDL_LANG=zh_CN.UTF-8
        initrd $idfile
    }
 
__eof__
cat ${tmpfile} >> $efi_cfg
cat ${tmpfile} >> $pc_cfg
if test -f ${grub_menu};then
cat <<__eof__ >>${grub_menu_tmp}

title CDLinux
        find --set-root  --ignore-floppies  $idfile
        kernel /CDlinux/bzImage CDL_LANG=zh_CN.UTF-8
        initrd $idfile
__eof__
fi

fi
rm -f ${tmpfile}




### MaxDOS
idfile=/[bB][oO][Oo][tT]/[Mm][Aa][xX][dD][oO][sS].[iI][mM][gG]
tmpfile=/tmp/.tmp_grub.cfg
if iso_root_exists $idfile  && test -f ${grub_menu};then
cat <<__eof__ >>${grub_menu_tmp}

title MaxDOS
    find --set-root --ignore-floppies $idfile
    map --mem $idfile (fd0)
    map --hook
    chainloader (fd0)+1
    rootnoverify (fd0)
    map --floppies=1

__eof__
    if test -f ${ISO_ROOT}/isolinux/memdisk ;then

cat <<__eof__ >>${tmpfile}
    menuentry "MaxDOS" {
        search --file --no-floppy --set=root $idfile
        linux16 /isolinux/memdisk  raw
        initrd16 $idfile
    }
__eof__

    fi
fi
cat ${tmpfile} >> $pc_cfg
rm -f ${tmpfile}



if test -f ${grub_menu};then
    echo "####insert by genrubcfg  --------end" >>${grub_menu_tmp}

    sed -r -i "/##INSERT##/r ${grub_menu_tmp}" ${grub_menu}
    sed  -r -i "/##INSERT##/d"  ${grub_menu}
fi

rm -f ${grub_menu_tmp}

cat <<__end__ >${ISO_ROOT}/isolinux/isolinux.cfg || exit 2
prompt 1
##TIMEOUT 5
default grub2

label grub4dos
  menu label ^Grub4Dos
  kernel  /grub4dos/grub.exe 
  append --config-file="errorcheck off;configfile (cd)${GRUB4DOS_MENU};find --set-root --ignore-floppies --ignore-cd ${GRUB4DOS_MENU} && configfile ${GRUB4DOS_MENU};errorcheck on"

label grub2
  menu label ^Grub2
  kernel ${GRUB2_PREFIX}/g2ldr
__end__
